package com.qianniu.napi.admin.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.English;
import com.qianniu.napi.admin.service.IGoodsService;
import com.qianniu.napi.common.util.DateUtil;
import com.qianniu.napi.common.util.FileUtil;
import com.qianniu.napi.common.util.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Configuration
@Component // 此注解必加
@EnableScheduling // 此注解必加
public class DailyTask {

    private static final Logger logger = LoggerFactory.getLogger(DailyTask.class);

    @Autowired
    private IGoodsService goodsService;

//    @Scheduled(cron = "0 50 1 * * ?")
    public void prepareEnglish() {

//        String url = "http://news.iciba.com/views/dailysentence/daily.html#!/detail/title/DATE";
        String url = "http://sentence.iciba.com/index.php?callback=&c=dailysentence&m=getdetail&title=DATE&_=1500205055500" ;
        String path = "/fafa/www/images/english/" ;

        String startdate = "2019-01-30" ;
//        String startdate = "2018-03-16" ;
//        Date stdate = DateUtil.toDate(startdate, "yyyy-MM-dd");
        String today = DateUtil.getToday();
        Date stdate = DateUtil.toDate(today, "yyyy-MM-dd");
        Boolean flag = true;
        int n = 1;
//        while (flag) {
        String datestr = DateUtil.toDateString(DateUtil.addDay(stdate, n), "yyyy-MM-dd");

        String tmpurl = url.replace("DATE", datestr);
        String result = HttpUtils.httpGet(tmpurl);
        if (Strings.isNullOrEmpty(result)) {
            return;
        }

        JSONObject json = JSON.parseObject(result);

        String imgurl = json.getString("picture2").length() <= 0 ?
                json.getString("picture") : json.getString("picture2");
        String tmppath = "" ;
        if (Strings.isNullOrEmpty(imgurl)) {
            return;
        }

        tmppath = path + datestr + ".jpg" ;
        FileUtil.download(imgurl, tmppath, true);
        try {
            Runtime.getRuntime().exec("chmod 644 " + tmppath);
        } catch (IOException e) {
        }

        English english = new English();
        english.setChinese(json.getString("note").replace("词霸小编：", ""));
        english.setEnglish(json.getString("content"));
        english.setNote(json.getString("translation").replace("词霸小编：", ""));
        english.setPdate(datestr);
        english.setPath(tmppath.replace("/fafa/www/images", "https://img.niucaiduo.com"));

        JSONArray arr = JSON.parseArray(json.getString("tags"));
        String tags = "" ;
        if (arr != null && arr.size() > 0) {
            for (int i = 0; i < arr.size(); i++) {
                tags += arr.getJSONObject(i).getString("tagname") + "、" ;
            }
        }
        english.setTags(tags.length() == 0 ? "" : tags.substring(0, tags.length() - 1));
        english.insert();
//            n++;
//            flag = DateUtil.toDate(datestr,"yyyy-MM-dd").getTime() <= DateUtil.toDate(today,"yyyy-MM-dd").getTime();

//        }

    }


}
